package com.ebid.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.sun.javafx.collections.MappingChange;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class LoopJsonTest {

    private static Map map = new HashMap();

    public static void jsonLeaf(JsonNode node,Element element)
    {
        if (node.isValueNode())
        {
//            System.out.println(node.toString());
//            element.setText(node.toString().substring(1,node.toString().length()-1));
            return;
        }

        if (node.isObject())
        {
            Iterator<Map.Entry<String, JsonNode>> it = node.fields();
            while (it.hasNext())
            {
                Map.Entry<String, JsonNode> entry = it.next();
                System.out.println(entry.getKey());

                if(entry.getValue() != null){
                    map.put(entry.getKey(),entry.getValue().toString());
                    jsonLeaf(entry.getValue(),element);
                }

            }
        }

        if (node.isArray())
        {
            if (node.size()==0){
                return;
            }
            Iterator<JsonNode> it = node.iterator();
            while (it.hasNext())
            {
                jsonLeaf(it.next(),element);
            }
        }
    }


    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }

    public static void main(String[] args) {

        Document documentNew = DocumentHelper.createDocument();
        documentNew.setXMLEncoding("UTF-8");
        Element rootElement = documentNew.addElement("qualifyBulletinPhase");

        //  jackson
        try
        {
            File file = new File("D:/jsonnn.txt");
            String ss = txt2String(file);
            ObjectMapper jackson = new ObjectMapper();
            JsonNode node = jackson.readTree(ss);
            jsonLeaf(node,rootElement);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        //5.设置生成xml的格式
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");

        //6.生成xml文件
        File file = new File("qualifyBulletinPhase.xml");
        StandaloneWriter writer;
        try {
            writer = new StandaloneWriter(new FileOutputStream(file), format);
            //设置是否转义，默认值是true，代表转义
            writer.setEscapeText(false);
            writer.write(documentNew);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }



        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println("key: "+entry.getKey()+" value:"+entry.getValue());
        }
    }
}
